package cn.wolfcode.web.controller;


import cn.wolfcode.domain.Exam;
import cn.wolfcode.domain.ExamQuestion;
import cn.wolfcode.domain.Question;
import cn.wolfcode.domain.QuestionItem;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IExamQuestionService;
import cn.wolfcode.service.IExamService;
import cn.wolfcode.service.IQuestionItemService;
import cn.wolfcode.service.IQuestionService;
import cn.wolfcode.util.RequiredPermission;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("/exam")
public class ExamController {

    @Autowired
    private IExamService examService;
    @Autowired
    private IQuestionService questionService;
    @Autowired
    private IQuestionItemService questionItemService;
    @Autowired
    private IExamQuestionService examQuestionService;

    @RequiredPermission(name = "试卷页面", expression = "exam:list")
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") QueryObject qo) {

        List<Question> questions = questionService.listAll();
        qo.setPageSize(0);
        PageInfo<Exam> pageInfo = examService.query(qo);
        model.addAttribute("PageInfo", pageInfo);
        model.addAttribute("questions", questions);
        return "exam/list";
    }

    @RequestMapping("/input")
    public String input(Long id, Model model, Exam exam, Long type1, Long type2, Long type3, String score3, String score1, String score2) {
        //插入数据
        examService.save(exam);
        //随机出来的题目的id
        List<Question> questions1 = questionService.selectQuestion1(type1);
        List<Question> questions2 = questionService.selectQuestion2(type2);
        List<Question> questions3 = questionService.selectQuestion3(type3);

        //遍历id,获取对应的题目答案
        for (Question q : questions1) {
            Long id1 = q.getId();
            List<QuestionItem> questionItems = questionItemService.selectAnswer(id1);
            q.setQuestionItem(questionItems);
        }

        for (Question q : questions2) {
            Long id1 = q.getId();
            List<QuestionItem> questionItems = questionItemService.selectAnswer(id1);
            q.setQuestionItem(questionItems);
        }

        for (Question q : questions3) {
            Long id1 = q.getId();
            List<QuestionItem> questionItems = questionItemService.selectAnswer(id1);
            q.setQuestionItem(questionItems);
        }
        model.addAttribute("single", questions1);
        model.addAttribute("mulity", questions2);
        model.addAttribute("judge", questions3);
        model.addAttribute("exam", exam);
        model.addAttribute("score1", score1);
        model.addAttribute("score2", score2);
        model.addAttribute("score3", score3);

        ExamQuestion examQuestion1 = new ExamQuestion();
        ExamQuestion examQuestion2 = new ExamQuestion();
        ExamQuestion examQuestion3 = new ExamQuestion();
        for (int i = 1; i <= questions1.size(); i++) {
            examQuestion1.setExamId(exam.getId());
            QuestionItem q = questions1.get(i - 1).getQuestionItem().get(i - 1);
            examQuestion1.setQuestionId(questions1.get(i - 1).getQuestionItem().get(i - 1).getQuestionId());
            examQuestion1.setScore(20);
            examQuestion1.setSequence(i);
            examQuestionService.save(examQuestion1);
        }

        for (int i = 1; i <= questions2.size(); i++) {
            examQuestion2.setExamId(exam.getId());
            examQuestion2.setQuestionId(questions2.get(i - 1).getId());
            examQuestion2.setScore(20);
            examQuestion2.setSequence(i);
            examQuestionService.save(examQuestion2);
        }

        for (int i = 1; i <= questions3.size(); i++) {
            examQuestion3.setExamId(exam.getId());
            examQuestion3.setQuestionId(questions2.get(i - 1).getId());
            examQuestion3.setScore(20);
            examQuestion3.setSequence(i);
            examQuestionService.save(examQuestion3);
        }
        return "exam/input";
    }

    @RequestMapping("/show")
    public String show() {
        return "exam/input";
    }
}
